#!/usr/bin/python
# -*- coding: utf-8 -*-

__date__ = u"2 avr. 2010"
__author__ = u"INRIA pyLot"
__all__ = [u"StringList",
           u"SimpleStringList",
           u"Vector"]


from _pylot.core.utils import is_iterable, check_type, error
from _pylot.core.system import  ENCODING

class StringList(list):
  u"""
  StringList is a list of strings 

  >>> v = StringList([u'abc',u'111'])
  >>> v
  StringList(u'abc', u'111')
  >>> v = StringList(u'abc',u'222')
  >>> v
  StringList(u'abc', u'222')
  >>> w = StringList(v)
  >>> w
  StringList(u'abc', u'222')

  >>> v = StringList('abc')
  Traceback (most recent call last):
  ...
  TypeError: E005 - StringList: unicode expected, got tuple
  >>> v = StringList(u'abc',444)
  Traceback (most recent call last):
  ...
  TypeError: E005 - StringList: unicode expected, got int
  
  """
  def __init__(self, *values):
    if len(values) == 0 :
      list.__init__(self)
      vals = []
    elif len(values) == 1:
      val = values[0]
      if isinstance(val , unicode):
        vals = [val]
      else :
        try :#val iterable ?
          iter(val)
          self.__init__(*val)
          return
        except TypeError :
          raise TypeError, error(tr_(u"E005 - StringList: unicode expected, got %s") % type(values).__name__)
    else :
      vals = []
      for val in values :
        check_type(u'StringList', val, unicode)
        vals.append(val)

#    value = list(vals)
    list.__init__(self, vals)

  def __repr__ (self) :
    lst = [repr(val) for val in self]
    return u"StringList(%s)" % u", ".join(lst)

  def __unicode__ (self) :
    lst = [repr(val) for val in self]
    return u'StringList(%s)' % (u', '.join(lst))

  def __str__ (self):
    return unicode(self).encode(ENCODING)

  def topyLotStr (self):
    return u' '.join([unicode(val) for val in self])

class SimpleStringList (list):
  u"""
  SimpleStringList is a list of strings without spaces

  >>> v = SimpleStringList([1,u'a',True])
  >>> v
  SimpleStringList(1, a, True)
  >>> v[0]
  u'1'
  >>> SimpleStringList(SimpleStringList(1, 2, 3))
  SimpleStringList(1, 2, 3)
  """
  def __init__(self, *values):
    if len(values) == 0 :
      list.__init__(self)
    else :
      vals = []
      for val in values :
        if type(val) is SimpleStringList :
          vals = val
          break
        elif is_iterable(val) :
          vals = list(val)
          break
        elif type(val) == unicode :
          vals = val.split()
        else :
          vals.append(val)
      value = [unicode(val) for val in vals]
      list.__init__(self, value)

  def __repr__ (self) :
    lst = [unicode(val) for val in self]
    return u'SimpleStringList(%s)' % u', '.join(lst)

  def __unicode__ (self) :
    lst = [unicode(val) for val in self]
    return u'SimpleStringList(%s)' % (u','.join(lst))

  def __str__ (self):
    return unicode(self).encode(ENCODING)

  def topyLotStr (self):
    return u' '.join([unicode(val) for val in self])

class Vector (list):
  u"""
  Vector is a list of float

  >>> Vector([1,2,3])
  Vector(1.0, 2.0, 3.0)
  >>> Vector (1, 2, 3)
  Vector(1.0, 2.0, 3.0)
  >>> Vector ((1, 2, 3))
  Vector(1.0, 2.0, 3.0)
  >>> Vector (u"1 2 3")
  Vector(1.0, 2.0, 3.0)
  >>> Vector(); Vector([])
  Vector()
  Vector()
  >>> n = Vector(1, 2, 3)
  >>> Vector(n)
  Vector(1.0, 2.0, 3.0)
  >>> unicode(n)
  u'Vector(1.0, 2.0, 3.0)'
  """

  def __init__(self, *values):
    if len(values) == 0 :
      list.__init__(self)
    else :
      vals = []
      for val in values :
        if type(val) is Vector :
          vals = val
          break
        elif is_iterable(val) :
          vals = list(val)
          break
        elif type(val) == unicode :
          vals = val.split()
        else :
          vals.append(val)
      value = [float(val) for val in vals]
      list.__init__(self, value)

  def __repr__ (self) :
    lst = [unicode(val) for val in self]
    return u'Vector(%s)' % u', '.join(lst)

  def __unicode__ (self) :
    lst = [unicode(val) for val in self]
    return u'Vector(%s)' % (u', '.join(lst))

  def __str__ (self):
    return unicode(self).encode(ENCODING)

  def topyLotStr (self):
    return u' '.join([unicode(val) for val in self])

if __name__ == '__main__':
  import doctest
  doctest.testmod()
